package com.example.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.mapper.CasesMapper;
import com.example.demo.mapper.CasesTypeMapper;
import com.example.demo.pojo.Cases;
import com.example.demo.pojo.CasesType;
import com.example.demo.utils.LayuiResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/end/cases")
public class EndCasesController {


    @Resource
    private CasesMapper casesMapper;

    @Resource
    private CasesTypeMapper casesTypeMapper;

    @RequestMapping("/delete/{id}")
    public Object delete(@PathVariable Integer id){
        Cases cases = new Cases();
        cases.setId(id);
        cases.setStatus(0);
        casesMapper.updateById(cases);
        return LayuiResult.deleteSuccess();
    }


    @RequestMapping("/update")
    public Object update(@RequestBody Cases cases){
        if(cases.getCasesStatus() != null && cases.getCasesStatus().equals("案件已审结")){
            cases.setCasesEndTime(new Date());
        }
        casesMapper.updateById(cases);
        return LayuiResult.updateSuccess();
    }


    @RequestMapping("/get")
    public Object get(Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        QueryWrapper<Cases> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1);
        List<Cases> cases = casesMapper.selectList(queryWrapper);
        Integer count = casesMapper.selectCount(queryWrapper);
        PageInfo<Cases> pageInfo = new PageInfo<>(cases);
        pageInfo.getList().forEach(cases1 -> {
            cases1.setCasesTypeName(casesTypeMapper.selectById(cases1.getCasesTypeId()).getTypeName());
        });
        return LayuiResult.selectSuccess(pageInfo.getList(),count);
    }

    @RequestMapping("/search/{casesPlaintiffIdCard}")
    public Object search(@PathVariable String casesPlaintiffIdCard, Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        QueryWrapper<Cases> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1).eq("cases_plaintiff_id_card",casesPlaintiffIdCard);
        List<Cases> cases = casesMapper.selectList(queryWrapper);
        Integer count = casesMapper.selectCount(queryWrapper);
        PageInfo<Cases> pageInfo = new PageInfo<>(cases);
        pageInfo.getList().forEach(cases1 -> {
            cases1.setCasesTypeName(casesTypeMapper.selectById(cases1.getCasesTypeId()).getTypeName());
        });
        return LayuiResult.searchSuccess(pageInfo.getList(),count);
    }


    @RequestMapping("/approve/{id}")
    public Object approve(@PathVariable Integer id){
        Cases cases = casesMapper.selectById(id);
        if(cases.getCasesStatus().equals("案件已审结")){
            return LayuiResult.LayuiData(0,"该案件已审结，请勿重复操作",null);
        }
        if(cases.getCasesStatus().equals("审核未通过")){
            return LayuiResult.LayuiData(101,"该案件审核未通过，无法审批",null);
        }
        return LayuiResult.LayuiData(201,"审批案件",null);
    }

}
